﻿namespace MicroCloud.Authorization
{
    /// <summary>
    /// 从程序集中提取的模块信息载体，包含模块基本信息和模块依赖的功能信息集合
    /// </summary>
    [DebuggerDisplay("{ToDebugDisplay()}")]
    public class ModuleInfo : IEntityHash
    {
        #region 属性
        /// <summary>
        /// 获取或设置 模块代码
        /// </summary>
        public string Code { get; set; }
        
        /// <summary>
        /// 获取或设置 模块名称
        /// </summary>
        public string Name { get; set; }
        
        /// <summary>
        /// 获取或设置 顺序编号
        /// </summary>
        public decimal OrderNo { get; set; }
        
        /// <summary>
        /// 获取或设置 模块位置
        /// <para>父模块Code以点号 . 相连的字符串。</para>
        /// </summary>
        public string Position { get; set; }

        /// <summary>
        /// 获取或设置 父级模块名称
        /// <para>需要创建父级模块的时候设置值</para>
        /// </summary>
        public string PositionName { get; set; }

        /// <summary>
        /// 获取或设置 依赖功能
        /// </summary>
        public IFunction[] DependOnFunctions { get; set; } = Array.Empty<IFunction>();

        /// <summary>
        /// 获取 位置全名
        /// </summary>
        public string FullCode => $"{Position}.{Code}";

        #endregion

        #region Overrides
        /// <summary>
        /// 确定指定的对象是否等于当前对象
        /// <para>Determines whether the specified object is equal to the current object.</para>
        /// </summary>
        /// <param name="obj">要与当前对象进行比较的对象</param>
        /// <returns>如果指定的对象等于当前对象，则为true；否则为false。</returns>
        public override bool Equals(object obj)
        {
            if (obj is not ModuleInfo info)
            {
                return false;
            }

            return $"{info.Position}.{info.Code}" == $"{Position}.{Code}";
        }

        /// <summary>
        /// 用作默认哈希函数
        /// <para>Serves as the default hash function.</para>
        /// </summary>
        /// <returns>当前对象的哈希代码</returns>
        public override int GetHashCode()
        {
            return HashCode.Combine(Position, Code);
        }

        /// <summary>
        /// 调试显示
        /// </summary>
        /// <returns></returns>
        private string ToDebugDisplay()
        {
            return $"{Name}[{Code}]({Position}),FunctionCount:{DependOnFunctions.Length}";
        }

        #endregion
    }
}
